package com.winit.openapi.util;

import java.util.ArrayList;
import java.util.List;

import com.winit.common.orm.mybatis.PageBase;
import com.winit.common.query.Page;

/**
 * @version <pre>
 * Author	Version		Date		Changes
 * xianjun.wu 	1.0  		2015-1-9 	Created
 * 
 * </pre>
 * @since 1.
 */
public class BeanUtils {

    public static <T, E> void copyProperties(T source, E target, String[] ignoreParams) {
        copy(source, target, ignoreParams);
    }

    public static <T, E> void copyProperties(T source, E target) {
        copy(source, target, null);
    }

    public static <T, E> E copyProperties(T source, Class<E> type) {
        if (source == null) return null;
        E target;
        try {
            target = type.newInstance();
            copyProperties(source, target, null);
            return target;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static <T, E> E copyProperties(T source, Class<E> type, String[] ignoreParams) {
        E target;
        try {
            target = type.newInstance();
            copyProperties(source, target, ignoreParams);
            return target;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static <T, E> E copy(T source, E target, String[] ignoreParams) {
        if (source == null || target == null) return null;
        org.springframework.beans.BeanUtils.copyProperties(source, target, ignoreParams);
        return target;
    }

    public static <T, E> List<E> copyList(List<T> source, Class<E> type) {
        if (source == null || type == null) return null;
        List<E> result = new ArrayList<E>();
        E temp = null;
        for (T o : source) {
            temp = BeanUtils.copyProperties(o, type);
            result.add(temp);
        }
        return result;
    }

    public static <T, E> List<E> copyList(List<T> source, Class<E> type, String[] ignoreParams) {
        if (source == null || type == null) return null;
        List<E> result = new ArrayList<E>();
        E temp = null;
        for (T o : source) {
            temp = BeanUtils.copyProperties(o, type, ignoreParams);
            result.add(temp);
        }
        return result;
    }

    public static <T, E> Page<E> copyPageList(PageBase<T> source, Class<E> targetType) {
        return copyPageList(source, targetType, null);
    }

    public static <T, E> Page<E> copyPageList(PageBase<T> source, Class<E> targetType, String[] ignoreParams) {
        if (source == null || targetType == null) return null;

        List<E> list = new ArrayList<E>();
        E temp = null;
        for (T o : source) {
            temp = BeanUtils.copyProperties(o, targetType, ignoreParams);
            list.add(temp);
        }
        Page<E> result = new Page<E>(list, source.getPageable(), source.getTotalElements());
        return result;
    }
}
